<?php
/* 
 * Copyright (c) 2018-2021   All rights reserved.
 *
 * 创建时间：2021-10-31 18:59:50
 *
 * 项目：/levfm  -  $  - Form_levfm_formsController.php
 *
 * 作者：{{AUTO GENERATE}}
 */

//此文件使用程序自动生成，下次生成时【不会】覆盖。
//在这里可以补充、完善你的程序逻辑

namespace modules\levfm\controllers;

use Lev;
use lev\base\Adminv;
use lev\base\Assetsv;
use lev\base\Controllerv;
use lev\base\Viewv;
use modules\levfm\table\form\Form_levfm_fields\Form_levfm_fields;
use modules\levfm\table\form\Form_levfm_forms\Form_levfm_forms;

!defined('INLEV') && exit('Access Denied LEV');


Adminv::checkAccess();
Assetsv::registerSuperman();

class Form_levfm_formsController extends Controllerv
{

    /**
     * Renders the index view for the module
     * @see Modelv::findOne()
     * @see Modelv::inputsSetup()
     * @see Modelv::setupDesc()
     */
    public static function actionIndex() {
        $formPre = 'datax';

        $route = parent::$route;

        if (Lev::POSTv('dosubmit')) {
            echo json_encode(Form_levfm_forms::saveForm($formPre, $route));
            return;
        }

        $opid = floatval(Lev::GETv('opid'));
        $setup = Lev::stripTags(Lev::GETv('setup'));

        $formid = floatval(Lev::GETv('formid'));
        if ($setup == 10) {
            parent::redirect(Lev::toReRoute(['admin-fields', 'formid'=>$opid, 'id'=>'levfm']));
        }

        $opInfo = $opid >0 ? Form_levfm_forms::findOne(['id'=>$opid]) : [];
        //$inputs = levModulesModelHelper::inputs();
        $inputsSetup = Form_levfm_forms::inputsSetup();
        $setupDesc = Form_levfm_forms::setupDesc();
        $inputs = isset($inputsSetup[$setup]) ? $inputsSetup[$setup] : reset($inputsSetup);

        if ($opInfo) {
            $opInfo = Form_levfm_forms::setFormSettings($opInfo['settings'], $opInfo);
            //$opInfo['fields'] = Form_levfm_fields::findAll(['formid'=>$formid]);
            Lev::$app['title'] = '编辑：'.$opInfo['id'];
        }else {
            Lev::$app['title'] = '创建';
        }

        Assetsv::animateCss();
        Viewv::render('form/form_setup', [//所有form表单公用模板 - 如需修改请重新创建
            'opid'         => $opid,
            'inputs'       => $inputs,
            'extInputs'    => Form_levfm_forms::extInputs(),
            'setupDesc'    => $setupDesc,
            'inputsValues' => $opInfo,
            'formPre'      => $formPre,
            'setup'        => $setup,
            'addurl'       => Lev::toReRoute([$route]),

            'headerHtm' => Form_levfm_forms::headerHtm(),
            'footerHtm' => Form_levfm_forms::footerHtm(),
            'footerFormInnerHtm' => Form_levfm_forms::footerFormInnerHtm(),
        ]);

    }
}